package locCounter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileProcessor {
	private ArrayList<JavaFile> files;
	private boolean pFlag;
	private boolean previousComment; 		// Was there a comment in the line before? e.g /* ...
	
	public FileProcessor(String[] parameters) {
		pFlag = parseFlag(parameters);
		files = new ArrayList<JavaFile>();
		previousComment = false;
		
		for(String s: parameters) {
			if( !pFlag && (s == "-p" || s == "-P") )
				pFlag = true;
			if(s.endsWith(".java"))
				files.add(new JavaFile(s));
		}
		
		System.out.print("*Verbose mode is ");
		if (pFlag) System.out.print("on!*\n\n");
		else System.out.print("off!*\n\n");
		
		try {
			parseFiles();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	/**
	 *	Search for flag -p
	 */
	public boolean parseFlag(String[] argv) {

		for(String s: argv) {
			if (s.equals("-p") || s.equals("-P"))
				return true;
		}
		return false;
	}
	
	public void parseFiles() throws IOException {	
		
		for(JavaFile f: files) {
			if (parseFile(f))		// Error occurred
				files.remove(f);
		}
	}
	
	public boolean parseFile(JavaFile file) throws IOException {

		int count = 0;
        Scanner input;
		try {
			System.out.print("-----" + file.getFileName() + "-----\n");
			input = new Scanner(new FileInputStream(file.getFileName()));
	        while (input.hasNextLine()) {
                String line = input.nextLine();
                
                count += (lineCounts(line, pFlag) ? 1 : 0);
	        }
		} catch (FileNotFoundException e) {
			e.printStackTrace();
        }
     	file.setNumberLines(count);

		System.out.print(file.getFileName() + ": " + file.getNumberLines() + " LOC\n\n");
		return false;
	}
	
	private boolean lineCounts(String line, boolean pFlag) {
		
		Pattern comments = 
				Pattern.compile("(//.*)|(/\\*(.*)|(.*)(?:[\\n\\r])*?\\*/)");
		
		Pattern blankLines = 
			Pattern.compile("[\\n\\r\\s][\\n\\r\\s]+");

        Matcher matcher = comments.matcher(line);
        if (line.contains("/*") && !line.contains("*/")) { // Dirty way to check if there was a multi line
        	previousComment = true;						   // initiated.
        }
        
		line = matcher.replaceAll("");
		
		if (!matcher.matches() && previousComment) {
			comments = Pattern.compile(".*\\*/");
			matcher = comments.matcher(line);
			if (matcher.matches()) {
				previousComment = false; 	   // "/*" comment closed
				line = matcher.replaceAll(""); // Erase everything before 
			}
		}
		
		matcher = blankLines.matcher(line);
		line = matcher.replaceAll("");
		
        if( pFlag & !line.isEmpty())
        	System.out.print(line + "\n");
        
		return (!line.isEmpty());	
	}

	public ArrayList<JavaFile> getFiles() {
		return files;
	}

	public boolean ispFlag() {
		return pFlag;
	}

	public void setpFlag(boolean pFlag) {
		this.pFlag = pFlag;
	}
	
	 @Override
	 public String toString() {
		 
		StringBuilder toReturn = new StringBuilder(); 		
		int count = 0;
		for(JavaFile f: files) {
			count += f.getNumberLines();
		}
		
		toReturn.append("Total: " + count + " LOC \n");
		
		return toReturn.toString();
	 }
}
